import 'package:flutter/material.dart';
import 'package:banquanjie_app/app/services/screenAdapter.dart';
import 'package:get/get.dart';

class Customscaffold extends StatelessWidget {
  final String title;
  final bool showBackButton;
  final bool showDrawerButton;
  final Widget? child;
  
  // 添加GlobalKey来控制Scaffold
  final GlobalKey<ScaffoldState> _scaffoldKey = GlobalKey<ScaffoldState>();
  
  Customscaffold({
    super.key,
    required this.title,
    required this.child,
    this.showBackButton = true,
    this.showDrawerButton = true,
  });
  
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      key: _scaffoldKey, // 添加key
      appBar: AppBar(
        title: Text(title, style: TextStyle(fontSize: ScreenAdapter.fontSize(34), fontWeight: FontWeight.w600)),
        centerTitle: true,
        automaticallyImplyLeading: false, // Disables system back button
        leading: showDrawerButton == false && showBackButton == false ? Text('') :  _buildLeadingCapsule(context), // Your custom capsule
        leadingWidth: ScreenAdapter.width(172),
      ),
      drawer: Drawer(
        width: ScreenAdapter.width(358),
        backgroundColor: Colors.white,
        child: ListView(
          padding: EdgeInsets.only(left: ScreenAdapter.width(72)),
          children: [
            ListTile(title: Text('首页', style: TextStyle(fontSize: ScreenAdapter.fontSize(30))),onTap: () {}),
            ListTile(title: Text('会员库', style: TextStyle(fontSize: ScreenAdapter.fontSize(30))),onTap: () {}),
            ListTile(title: Text('经典IP', style: TextStyle(fontSize: ScreenAdapter.fontSize(30))),onTap: () {}),
            ListTile(title: Text('作品项目', style: TextStyle(fontSize: ScreenAdapter.fontSize(30))),onTap: () {}),
            ListTile(title: Text('品牌公司', style: TextStyle(fontSize: ScreenAdapter.fontSize(30))),onTap: () {}),
            ListTile(title: Text('IP精英', style: TextStyle(fontSize: ScreenAdapter.fontSize(30))),onTap: () {}),
            ListTile(title: Text('IP精英', style: TextStyle(fontSize: ScreenAdapter.fontSize(30))),onTap: () {}),
            ListTile(title: Text('服务协议', style: TextStyle(fontSize: ScreenAdapter.fontSize(30))),onTap: () {}),
            ListTile(title: Text('意见反馈', style: TextStyle(fontSize: ScreenAdapter.fontSize(30))),onTap: () {}),
            ListTile(title: Text('联系我们', style: TextStyle(fontSize: ScreenAdapter.fontSize(30))),onTap: () {}),
          ]
        )
      ),
      body: child,
    );
  }
  
  Widget _buildLeadingCapsule(context) {
    return Container(
      padding: EdgeInsets.only(left: ScreenAdapter.width(16)),
      alignment: Alignment.center,
      child: Container(
        width: ScreenAdapter.width(156),
        height: ScreenAdapter.width(64),
        // margin:  EdgeInsets.only(left: ScreenAdapter.width(16)),
        decoration: BoxDecoration(
          // color: Colors.yellow,
          border: Border.all(color: Color(0xFFF6F6F6), width: showBackButton == false ? 0 : 1),
          borderRadius: BorderRadius.circular(100),
        ),
        child: Row(
          mainAxisSize: MainAxisSize.min,
          children: [
            if (showBackButton)
              Expanded(
                flex: 1,
                child: IconButton(
                  padding: EdgeInsets.all(0),
                  icon: const Icon(Icons.arrow_back),
                  onPressed: () => Get.back(),
                ),
              ),
            Container(
              width: ScreenAdapter.width(1),
              height: ScreenAdapter.width(36),
              color: Color(0xFFDFDFDF)
            ),
            if (showDrawerButton)
              Expanded(
                flex: 1,
                child: IconButton(
                  padding: EdgeInsets.all(0),
                  icon: const Icon(Icons.menu),
                  onPressed: () => _scaffoldKey.currentState?.openDrawer(), // 使用GlobalKey打开drawer
                ),
              ),
          ],
        ),
      )
    );
  }
}